package com.chuangyang.fixboxmaster.ui.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chuangyang.fixboxmaster.AppData;
import com.chuangyang.fixboxmaster.R;
import com.chuangyang.fixboxmaster.bean.B2BPartDetailItem;
import com.chuangyang.fixboxmaster.bean.PriceItem;
import com.chuangyang.fixboxmaster.ui.widgets.PinnedSectionListView;

import java.util.ArrayList;

public class B2BPartDetailListAdapter extends BaseAdapter implements PinnedSectionListView.PinnedSectionListAdapter {

    private ArrayList<B2BPartDetailItem> mB2BPartList = new ArrayList<>();

    public B2BPartDetailListAdapter(ArrayList<B2BPartDetailItem> skills) {
        mB2BPartList = skills;
    }

    @Override
    public int getCount() {
        return mB2BPartList.size();
    }

    @Override
    public B2BPartDetailItem getItem(int position) {
        return mB2BPartList != null ? mB2BPartList.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(AppData.getContext()).inflate(R.layout.b2b_part_detail_list_item, parent, false);
            holder.mSection = (TextView) convertView.findViewById(R.id.section);
            holder.mItem = (LinearLayout) convertView.findViewById(R.id.item);
            holder.mItemTitle = (TextView) convertView.findViewById(R.id.item_title);
            holder.mItemPrice=(TextView)convertView.findViewById(R.id.item_price);
            holder.mAmount=(TextView)convertView.findViewById(R.id.item_amount);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        B2BPartDetailItem item = getItem(position);
        if (item != null) {
            if (item.type == PriceItem.SECTION) {
                holder.mSection.setVisibility(View.VISIBLE);
                holder.mItem.setVisibility(View.GONE);
                holder.mSection.setText(item.skillTitle);
            } else if (item.type == PriceItem.ITEM) {
                holder.mItem.setVisibility(View.VISIBLE);
                holder.mSection.setVisibility(View.GONE);
                holder.mItemTitle.setText(item.skillTitle);
                holder.mItemPrice.setText("￥"+item.price);
                holder.mAmount.setText(" x " + item.num);
            }
        }
        return convertView;
    }

    public static class ViewHolder {
        public TextView mSection;
        public LinearLayout mItem;
        public TextView mItemTitle;
        public TextView mItemPrice;
        public TextView mAmount;

    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        return getItem(position) != null ? mB2BPartList.get(position).type : 0;
    }

    @Override
    public boolean isItemViewTypePinned(int viewType) {
        return viewType == PriceItem.SECTION;
    }
}
